package com.example.mysharding.test;

import java.util.concurrent.*;

public class FutureExample {

    private static class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            // 模拟长时间的计算
//            Thread.sleep(20000);
            return "Hello, Future!";
        }
    }


    private static class MyRunnable implements Runnable{

        @Override
        public void run() {
            System.out.println("MyRunnable");
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建一个 ExecutorService
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        // 提交一个 Callable 任务并获取代表任务结果的 Future
        Future future = executorService.submit(new MyRunnable());

        System.out.println("Waiting for the result...");
        Thread.sleep(2000);
        // 获取并输出任务结果，如果任务还未完成，这个方法会阻塞等待
        Object o = future.get();
        System.out.println("Result: " + o);

        // 关闭 ExecutorService
        executorService.shutdown();
    }
}